<?php

session_start();
$col=':';
$stored_connection = $_POST["stored_connection"];
$connection_to_store = $_POST["connectionname"];

$dbhost = $_POST["hostname"];
$dbport = $_POST["port"];

$dbuser = $_POST["username"];
$dbpass = $_POST["password"];

$dbname = 'securich';

$_SESSION['dbname'] = $dbname;

$_SESSION['dbhost'] = $dbhost;
$_SESSION['dbport'] = $dbport;
$_SESSION['dbuser'] = $dbuser;
$_SESSION['dbpass'] = $dbpass;


function dbconnect($dbhostport_tmp, $dbuser_tmp, $dbpass_tmp)
{
  $con = @mysql_connect($dbhostport_tmp, $dbuser_tmp, $dbpass_tmp); // "@" is necessary.
  
  if (!$con)
    {
      $error=mysql_error();
      $_SESSION['error'] = $error;
      
      Header("Location: error.php");
    }
  else
    {
      Header("Location: welcome.php");
    }
}

function getvalues($stored_connection)
{
     $file = "connections.txt"; 

     $fp = fopen($file, "r") or die("Couldn't open $file for writing!");
          
     while((!feof($fp)) || ($flag != 1))
     {
        $line = fgets($fp);
        list($connection_name, $connection_settings) = explode("-:-" , $line);
        
        if ($connection_name == $stored_connection) {

          $flag=1;
          list($dbhost, $dbport, $dbuser) = explode("-_-" , $connection_settings);
        }        
     }
  
     fclose($fp);  
}

if ((!isset($_POST['submit'])) && (!isset($_POST['save']))) { // if page is not submitted to itself echo the form
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><HEAD><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<TITLE>SamMy - Securich GUI tool - Welcome</TITLE>

  <script type="text/javascript">

  function checkIt(el) 
  {
    var elArr = ['savesettings'];
    for (var i=0; i<elArr.length; i++) 
    {
  	  sel = document.getElementById(elArr[i]);
	  if (el.value == elArr[i]) { sel.style.display = 'block'; }
      else { sel.style.display = 'none'; } 
    }
    
  // not sure what this is supposed to do from source given ???    
  //    document.getElementById('who').value = '';
  }

// Format of StoreDetails()
// Name,Addr1,Addr2,Addr3,Phone,FAX,Email,Webpage
var StoreDetails = [
    ['Saved Connections','','',''],

     <?
        $file = "connections.txt"; 

        $fp = fopen($file, "r") or die("Couldn't open $file for writing!");
               
        while(!feof($fp))
        {
           $line = fgets($fp);
           list($connection_name, $connection_settings) = explode("-:-" , $line);
           list($arrayhostname, $arrayport, $arrayusername) = explode("-_-" , $connection_settings);
           echo "['" . $connection_name . "','" . $arrayhostname . "','" . $arrayport . "','" . $arrayusername . "'],";
        }
        
        fclose($fp);
     ?>
     
  ['','','',''] // Note: no comma
];

function Setup(TA) {
  var str = "<select id='Store' onchange='StoreInfo()'>";
  for (var i=0; i<StoreDetails.length; i++) {
    str += '<option value="'+StoreDetails[i].join('|')+'">'+StoreDetails[i][0]+'</option>';
  }
  str +='</select>';
  document.write(str);

}
function StoreInfo() {
  var sel = document.getElementById('Store').selectedIndex;
  var tmp = [];  tmp.push(sel);
  for (var i=1; i<4; i++) { tmp.push(StoreDetails[sel][i]); }
  document.getElementById('txhostname').value = tmp[1];
  document.getElementById('txport').value = tmp[2];
  document.getElementById('txusername').value = tmp[3];
}

</script>

<STYLE type="text/css" media="all">
	
	body {
		background: #f70;
		font: 0.8em arial, helvetica, sans-serif;
		margin: 0;
		padding: 0;
		height:100%;
        padding-bottom:35px;   /* Height of the footer */
	}
	
	#footer {
        position: absolute;
 	    bottom: 40px;
	    width:100%;
	    height:15px;   /* Height of the footer */
	    color: #fff;
    }
    
	#header {
		background-color: white;
	}
	
    #header ul {
		list-style: none;
		padding: 0;
		margin: 0;
		background: white;
		float: left;
		width: 100%;
    }
    
	#header li {
		float: left;
		margin: 0 1em 0 0;
    }
    
	#header a {
		text-decoration: none;
		display: block;
		width: 6em;
		padding: 0 0.5em;
		font-weight: bold;
		color: black;
		border-bottom: 0.5em solid #fc6;
		color: #fc6;
    }
	
	#header a:hover {
		color: #fa3;
		border-color: #fa3;
	}
	
	#header #selected a {
		color: #f80;
		border-color: #f80;
	}
	
	#content {
		clear: both;
		color: white;
		padding: 1em;
	}
	
	#content p {
		margin: 0 0 1em 0;
	}
	
	h1 {
		margin: 0;
		padding: 0.5em 0 1em 0.5em;
		color: #f80;
		font-size: 1.5em;
		font-style: italic;
	}
	</STYLE>
	
</HEAD><BODY>


<DIV id="header">

<H1>SamMy - The GUI frontend for Securich, the security plugin for MySQL</H1>

<UL>
	<LI id="selected"><A href="index.php">Connect</A></LI>
</UL>

</DIV>

<DIV id="content">
	<P>
<br><br>
<form method="post" action="<?php echo $PHP_SELF;?>">
<div align="center">
<TABLE BORDER="0" cellpadding="0" CELLSPACING="0">
<TR>
<TD>
<br><br><br><br><br><br><br><br>
<form name="theform" onsubmit="CheckForm()">
<table border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td width="150px">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" width="150px"><span class="text">Stored Connection:</span></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center" colspan="2"><script type="text/javascript">Setup();</script>
     <br>
    </td>
  </tr>
  <tr>
    <td align="left" width="150px"><span class="text">Hostname/IP:</span></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center" colspan="2"><input type="text" size="15" maxlength="100" id="txhostname" name="hostname"></td>
  </tr>
  <tr>
    <td align="left" width="150px"><span class="text">Port:</span></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center" colspan="2"><input type="text" size="15" maxlength="100" id="txport" name="port"></td>
  </tr>
  <tr>
    <td align="left" width="150px"><span class="text">Username:</span></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center" colspan="2"><input type="text" size="15" maxlength="36" id="txusername" name="username"></td>
  </tr>
  <tr>
    <td align="left" width="150px"><span class="text">Password:</span></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center" colspan="2"><input type="password" size="15" maxlength="36" name="password"></td>
  </tr>
  <tr>
    <td align="left" width="150px">&nbsp;</td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center"></td>
    <td align="center"></td>
  </tr>
  <tr>
    <td align="left" width="150px">&nbsp;</td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center"><input type="radio" name="radioBtn" value="savesettings" onclick="checkIt(this)">New</td>
    <td align="center"><input type="submit" value="Connect" name="submit"></td>
  </tr>
  <tr>
    <td align="left" width="150px">&nbsp;<br></td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center"></td>
    <td align="center"></td>
  </tr>
  <tr>
  <td colspan="4">
  <div id="savesettings" style="display:none" />
  <form method="post" action="<?php echo $PHP_SELF;?>">
  <div>
  <table width="100%">
   <tr>
    <td align="left" width="150px">Connection Name:</td>
    <td align="center"><c1><input type="text" id="multiple11" size="25" name="connectionname"/><br></c1></td>
    <td align="center"><input type="submit" value="Save" name="save"></td>
   </tr>
  </table>
  </div>
  </form>
  </div> 
  </td>
  </tr>
  <tr>
    <td align="left" width="150px">&nbsp;</td>
    <td align="left" width="20px">&nbsp;</td>
    <td align="center"></td>
    <td align="center"></td>
  </tr>
</table>
</form>

</TD>

</TR>
</TABLE>
</div>
<br />
<br /><br /><br />
</form>
	</P>
</DIV>

   <DIV id="footer" align="left">
   <table>
     <tr>
       <td width="23px">&nbsp;</td>
       <td>
         Copyright &copy; Darren Cassar a.k.a. preacher<br />
         Developed by Darren Cassar <?php echo date("Y"); ?> - GPLv2
       </td>
     </tr>
   </table>
   </DIV>
<?php
} else {

  if (isset($_POST['save'])) {
  
     $data = $connection_to_store . "-:-" . $dbhost . "-_-" . $dbport  . "-_-" . $dbuser . "-_-\n";
     $file = "connections.txt"; 

     $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
     fwrite($fp, $data) or die("Couldn't write values to file!"); 

     fclose($fp); 
  }
  
  

if ($dbhost=="localhost") {
   $dbhost = "127.0.0.1";
   }
   
   
//echo "connection_name= x" . $connection_name . "x<br>";
//echo "stored_connection= x" . $stored_connection . "x<br><br>";

//echo "hostname= x" . $dbhost . "x<br>";
//echo "port= x" . $dbport . "x<br>";
//echo "username= x" . $dbuser . "x<br>";
//echo "password= x" . $dbpass . "x<br>";

$_SESSION['dbname'] = $dbname;

$_SESSION['dbhost'] = $dbhost;
$_SESSION['dbport'] = $dbport;
$_SESSION['dbuser'] = $dbuser;
$_SESSION['dbpass'] = $dbpass;

$dbhostport = $dbhost . $col . $dbport; 

dbconnect($dbhostport, $dbuser, $dbpass);
} // end if  

?>

</BODY></HTML>